17. Switch to Using a ListView and Adapter Quiz

Question:

Modify the NumbersActivity onCreate() method:

  1. Remove the for loop which created a new TextView and added it to the layout for each word in the list.

  2. Add these 3 lines of code to the method, after the words list has been created:

    ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, words);
    
    ListView listView = (ListView) findViewById(R.id.list);
    
    listView.setAdapter(itemsAdapter);

To see what your NumbersActivity.java file should look like after this change, check out the github link here.

Modify the activity_numbers.xml layout file:

3 . Replace the whole file with this XML. It contains a single ListView element with view ID list.

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/list"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.example.android.miwok.NumbersActivity"/>

Start Quiz:

Solution: